/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.MaintainPartsAttributeRelation;
import org.springblade.modules.admin.vo.MaintainPartsAttributeRelationVO;
import org.springblade.modules.admin.mapper.MaintainPartsAttributeRelationMapper;
import org.springblade.modules.admin.service.IMaintainPartsAttributeRelationService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 配件_属性关联表 服务实现类
 *
 * @author ysq
 * @since 2024-10-12
 */
@Service
public class MaintainPartsAttributeRelationServiceImpl extends BaseServiceImpl<MaintainPartsAttributeRelationMapper, MaintainPartsAttributeRelation> implements IMaintainPartsAttributeRelationService {

	@Override
	public IPage<MaintainPartsAttributeRelationVO> selectMaintainPartsAttributeRelationPage(IPage<MaintainPartsAttributeRelationVO> page, MaintainPartsAttributeRelationVO maintainPartsAttributeRelation) {
		return page.setRecords(baseMapper.selectMaintainPartsAttributeRelationPage(page, maintainPartsAttributeRelation));
	}

	@TenantIgnore
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean addPartsAttributeRelation(Long id, List<Long> attributeIdList) {
		// 1. 先删除原有关联关系
		List<MaintainPartsAttributeRelation> existList = this.list(new LambdaQueryWrapper<MaintainPartsAttributeRelation>()
			.eq(MaintainPartsAttributeRelation::getPartsId, id)
			.eq(BaseEntity::getIsDeleted, 0)
		);
		boolean removeResult = true;
		if(Func.isNotEmpty(existList)){
			List<Long> existIdList = existList.stream().map(BaseEntity::getId).collect(Collectors.toList());
			removeResult = this.removeByIds(existIdList);
		}
		// 1.1 删除的情况
		if(Func.isEmpty(attributeIdList)){
			return removeResult;
		}
		// 2. 批量插入新关联关系
		List<MaintainPartsAttributeRelation> addList = new ArrayList<>();
		attributeIdList.forEach(a -> {
			MaintainPartsAttributeRelation item = new MaintainPartsAttributeRelation();
			item.setPartsId(id);
			item.setAttributeId(a);
			item.setTenantId("000000");
			addList.add(item);
		});
		boolean addResult = this.saveBatch(addList);
		if(!addResult && removeResult){
			throw new ServiceException("新增配件属性关联关系失败，请联系管理员");
		}
		return addResult && removeResult;

	}

}
